<?php
use Workerman\Worker;
require_once __DIR__ . '/Workerman/Autoloader.php';


$worker = new Worker('http://0.0.0.0:9999');

$worker->count = 4;


$worker->onWorkerStart = function($worker)
{
//    var_dump(__DIR__);
    error_reporting(E_ERROR | E_PARSE );

    define('APP_PATH', __DIR__ . '/application/');
    require __DIR__ . '/thinkphp/base.php';
//     加载框架引导文件
//    require __DIR__ . '/thinkphp/start.php';
};

$worker->onMessage = function($connection, $data)
{
    $_SERVER = [];

    //server信息
    if (isset($data['server'])) {

        foreach ($data['server'] as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }

    //header头信息
    if (isset($data['header'])) {
        foreach ($data['header'] as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }

    $_GET = [];
    //get请求
    if (isset($data['get'])) {
        foreach ($data['get'] as $k => $v) {
            $_GET[$k] = $v;
        }
    }

    $_POST = [];
    //post请求
    if (isset($data['post'])) {
        foreach ($data['post'] as $k => $v) {
            $_POST[$k] = $v;
        }
    }

    $_FILES = [];
    //文件请求
    if (isset($data->files)) {
        foreach ($data->files as $k => $v) {
            $_FILES[$k] = $v;
        }
    }

    $_COOKIE = [];
    //cookies请求
    if (isset($data->cookie)) {
        foreach ($data->cookie as $k => $v) {
            $_COOKIE[$k] = $v;
        }
    }
    ob_start();//启用缓存区

//    workman常住内存模式下每次单例启动清除Request对象
    \think\Request::destroy();
    \think\App::run()->send();
    $res = ob_get_contents();//获取缓存区的内容
    ob_end_clean();//清除缓存区

    //输出缓存区域的内容
    $connection->send($res);
};


// 运行worker
Worker::runAll();